﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WepApiTest1.Models;

namespace WepApiTest1.Controllers
{
    public class ContactController : ApiController
    {
        Contact[] contacts = new Contact[]
        {
            new Contact(){ ID=1,Age=20,Datetime=DateTime.Now,Name="哈哈",Sex="男"},
            new Contact(){ ID=2,Age=18,Datetime=DateTime.Today,Name="呵呵",Sex="女"},
              new Contact(){ ID=1,Age=20,Datetime=DateTime.MinValue,Name="拉拉",Sex="男"},
            new Contact(){ ID=2,Age=18,Datetime=DateTime.MaxValue,Name="⭐⭐",Sex="女"},
        };

        public IEnumerable<Contact> GetListAll()
        {
            return contacts;
        }

        //Post test
        public Contact PostContactByID(int id)
        {
            var contact = contacts.FirstOrDefault<Contact>(item => item.ID == id);

            if(contact == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound); //post返回404
            }

            return contact;
        }
        //Get test
        public IEnumerable<Contact> GetListBySex(string sex)
        {
            return contacts.Where(item => item.Sex == sex);
        }



    }
}
